//
//  RMSystemSettingVC.m
//  RMNote
//
//  Created by lanou3g on 16/4/28.
//  Copyright © 2016年 lxf. All rights reserved.
//

#import "RMSystemSettingVC.h"
#import "NetWorkRequestManager.h"
#import "RMLocationManager.h"
@interface RMSystemSettingVC ()

@property (weak, nonatomic) IBOutlet UILabel *weatherLabel;

@property (weak, nonatomic) IBOutlet UILabel *temperstureLabel;
@property (weak, nonatomic) IBOutlet UILabel *locationLabel;

@property (weak, nonatomic) IBOutlet UILabel *sportAdviceLabel;
@end

@implementation RMSystemSettingVC


-(void)viewDidLoad{
    
    

}
- (void)viewWillAppear:(BOOL)animated{
    [self getLocation];
}

- (void)getLocation{
    [[RMLocationManager defaultManager]getLocation_complete:^(NSString *location) {
        self.locationLabel.text = location;
        [self requestData];
    } error:^(NSError *error) {
        
        UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"定位失败" message:@"请前往系统设置中打开定位" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:@"好" style:UIAlertActionStyleCancel handler:nil];
        UIAlertAction * settingAction = [UIAlertAction actionWithTitle:@"设置" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            //UIApplicationOpenSettingsURLString 系统提供进入设置页面路径
            NSURL * url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
            // 判断是否能跳转设置页面
            if ([[UIApplication sharedApplication] canOpenURL:url]) {
                // 如果能,跳转
                [[UIApplication sharedApplication] openURL:url];
            }
        }];
        [alertController addAction:cancelAction];
        [alertController addAction:settingAction];
        [self presentViewController:alertController animated:YES completion:nil];
        NSLog(@"定位失败,%@",error);
    }];
}



-(void)requestData{

    NSString *str = self.locationLabel.text;
//   str =@"北京";
    if (str.length == 0) {
        return;
    }
   
    [NetWorkRequestManager requestWithType:POST urlString:kJHWeatherUrl parDic:@{@"cityname":str ,@"key":kJHWeatherAppKey } finish:^(NSData *data) {
      
        if (!data) {
            return ;
        }

        NSDictionary * dataDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];

        if (![[dataDic objectForKey:@"reason"] isEqualToString:@"successed!"]) {
            return ;
        }

       NSDictionary * resultDic  = [dataDic objectForKey:@"result"];
        
        if (!resultDic) {
            return;
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            
            self.sportAdviceLabel.text = [[[[[resultDic objectForKey:@"data"] objectForKey:@"life"] objectForKey:@"info"] objectForKey:@"yundong"]lastObject];
            self.weatherLabel.text = [[[[resultDic objectForKey:@"data"] objectForKey:@"realtime"] objectForKey:@"weather"] objectForKey:@"info"];
            
            NSString * string = [[[[resultDic objectForKey:@"data"] objectForKey:@"realtime"] objectForKey:@"weather"] objectForKey:@"temperature"];
            self.temperstureLabel.text = [NSString stringWithFormat:@"%@℃",string];
            
        });
        
        
    } error:^(NSError *error) {
        
        NSLog(@"error = %@",error);
        
    }];

}


//mark!!!:密码
- (IBAction)passWordAction:(id)sender {


    
    
}



@end
